﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ElectronicRecords.Models
{
    public class ProcedursRepository
    {
        private ElectronicRecordsDataContext _datacontext;

        public ProcedursRepository (ElectronicRecordsDataContext d)
        {
            _datacontext = d;
        }

        public IEnumerable<Procedurs> GetAllProcedurs()
        {
            if (_datacontext.Procedurs.Count<Procedurs>() != 0)
            {
                return _datacontext.Procedurs.OrderBy(p => p.Name);
            }
            else return _datacontext.Procedurs;
        }

        public Procedurs GetProcedure(int id)
        {
            return _datacontext.Procedurs.SingleOrDefault(p => p.ID_Procedure == id);
        }
       //не реализовано
       // public IEnumerable<Procedurs> GetProcedursBySpecialist

        public Procedurs CreateProcedure(Procedurs p)
        {
            _datacontext.Procedurs.InsertOnSubmit(p);
            _datacontext.SubmitChanges();
          
            return p;
        }
        
        public Procedurs UpdateProcedure(Procedurs p)
        {
            Procedurs dbProcedure = GetProcedure(p.ID_Procedure);
            dbProcedure.CreatedBy = p.CreatedBy;
            dbProcedure.Description = p.Description;
            dbProcedure.ID_Equipment = p.ID_Equipment;
           
            dbProcedure.Length = p.Length;
            dbProcedure.Name = p.Name;
            dbProcedure.Procedure_Specialist_XREF = p.Procedure_Specialist_XREF;
            
            _datacontext.SubmitChanges();
            return p;
        }

        public void DeleteProcedure(int id)
        {
            Procedurs p = GetProcedure(id);
            _datacontext.Procedurs.DeleteOnSubmit(p);
            _datacontext.SubmitChanges();
        }

    }
}